<?php
/**
 * Puts user input into system.
 *
 * All input validation must be done in here
 **/

require_once 'config.php';
require_once 'util/smurfs.php';

if( !is_string($_REQUEST['type']) ) {
  // error BREAK
} 

// function variables are tempting
//   however, pose risk and should be comtemplated
//   after a full review of standards has been set
//   to insure no code injection
switch( $_REQUEST['type'] ) {
  case 'POST': 
          handlePost(); 
          break;
  case 'THREAD':
          handleThread();
          break;
  case 'FORUM': 
          handleFora(); 
          break;
  default:
}

// //  abstraction is tempting, again we'll wait for optimization
function handlePost() {
  require_once 'db/db.php'; // for default_increment()
  require_once 'db/put/post.php';

  $fnum = 0; 
  $tnum = 0;

  if ( isset($_REQUEST['action']['new']) || isset($_REQUEST['action']['edit']) ){
    if( $_POST['tnum'] > 0 ) {
      $tnum = (int)intval($_POST['tnum']);
      if( $_POST['pnum'] > 0 ) { 
        $pnum = (int)intval($_POST['pnum']);
      } else {
        $pnum = -1;
      }
    } else if ( $_POST['fnum'] > 0 && strlen($_POST['title']) > 3 ) { // MAGIC NUMBER 5
      $tnum = (int)intval($_POST['fnum']);
      $pnum = 0;
    } else {
      // BREAKAGE
      die("ohz noes");
    }

    setPost( $tnum, $pnum, clean($_POST['title']), clean($_POST['datum']), array('getIds' => true) );
  
  } elseif( isset($_REQUEST['action']['delete']) 
            && isset($_REQUEST['pnum'])
            && isset($_REQUEST['tnum']) ) {
    deletePost( (int)intval($_REQUEST['tnum']), (int)intval($_REQUEST['pnum']) );  
  }

  // TODO back reffereal or to forum.php
  require_once 'tpl/use/Redirect.php';
  if ($tnum > 0) {
    // not exactly the most optimized way to get the start num
  $pnum = ($pnum > default_increment()) ? ((int)(($pnum-1)/default_increment()))*default_increment()
                                        : 0;
  skin_Redirect("spit.php?type=POST&tnum=${tnum}&start=${pnum}");
  } elseif ($fnum > 0)
    skin_Redirect("spit.php?type=THREAD&fnum=${fnum}");
  else
    skin_Redirect("spit.php");

  return true;
}

function handleThread() {
  require_once 'db/put/thread.php';

  if( isset($_REQUEST['action']['delete']) 
            && isset($_REQUEST['tnum'])
            && isset($_REQUEST['fnum']) ) {
    deleteThread( (int)intval($_REQUEST['fnum']), (int)intval($_REQUEST['tnum']) );  
  }

  require_once 'tpl/use/Redirect.php';
  skin_Redirect("spit.php");
  // TODO refferal or forum.php
}

function handleFora() {
  require_once 'db/put/forum.php';

  if( isset($_POST['title']) ) {
    $fnum = ($_POST['fnum'] > 0 ) ? (int)intval($_POST['fnum']) : -1;

    if( isset($_REQUEST['action']['new']) || isset($_REQUEST['action']['edit']) ) {
      setForum( -1, $fnum, clean($_POST['title']), clean($_POST['datum']) );
    } elseif (isset($_REQUEST['action']['delete'])) {
      deleteForum( -1, $fnum );
    } else {
      die("BREAKAGE");
    }
  }
  require_once 'tpl/use/Redirect.php';
  skin_Redirect("spit.php");

  // TODO refferal or to forum.php
}


?>

